package edu.neusoft.android.yuejian14.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;

import java.util.List;

import edu.neusoft.android.yuejian14.Listener.RetrofitListener;
import edu.neusoft.android.yuejian14.R;
import edu.neusoft.android.yuejian14.adapter.ClubListAdapter;
import edu.neusoft.android.yuejian14.javabean.Club;
import edu.neusoft.android.yuejian14.model.ClubModel;

public class SearchClubActivity extends AppCompatActivity  implements View.OnClickListener,RetrofitListener {

    private EditText keywordView;
    private ImageButton Search;
    private ImageButton Back;
    private RecyclerView recyclerView;
    private ClubListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_club);
        keywordView = (EditText) findViewById(R.id.search);
        Back = (ImageButton) findViewById(R.id.btn_back);
        Search = (ImageButton) findViewById(R.id.search_club);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView_search);

        Back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        Search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String keyword  = keywordView.getText().toString();
                searchClub(keyword);
            }
        });
    }

    private void searchClub(String clubName) {
        ClubModel clubModel = new ClubModel();
        clubModel.getClubBySearch(clubName,this);
    }

    @Override
    public void onSuccess(Object object, int flag) {
        List<Club> club = (List<Club>) object;
        adapter = new ClubListAdapter(SearchClubActivity.this,club);
        LinearLayoutManager layoutManager = new LinearLayoutManager(SearchClubActivity.this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
    }

    @Override
    public void onFail() {

    }

    @Override
    public void onClick(View view) {

    }
}
